#include <iostream>
#include <stdlib.h>
#include "Times.h"
#include "tanaman.h"
#include "classmenu.h"
#include "windows.h"
#include "Kurcaci.h"
#include "Aqua.h"
#include "Bolt.h"
#include "Clash.h"

using namespace std;

Times::Times()
{

}

Times::~Times()
{

}

int Times::GetDay() const
{
    return Day;
}

int Times::GetSeason() const
{
    return Season;
}

int Times::GetClock() const
{
    return Clock;
}

int Times::GetEvent() const
{
    return Event;
}

int Times::GetNormDay() const
{
    return NormalDay;
}

void Times::SetDay(int newDay)
{
    Day = newDay;
}

void Times::SetSeason(int newSeason)
{
    Season = newSeason;
}

void Times::SetEvent(int newEvent)
{
    int i, j, k;
    Event = newEvent;
    switch(Event)
    {
        case 1:
            for (i=0;i<=8;i++)
            {
                for(j=0;j<=8;j++)
                {
                    k = L.getStatus(i,j);
                    if (k==3)
                    {
                        L.setStatus(i,j,4);
                    }
                    else if (k==5)
                    {
                        L.setStatus(i,j,6);
                    }
                }
            }
            break;
        case 2:
            for (i=0;i<=8;i++)
            {
                for(j=0;j<=8;j++)
                {
                        k = rand ()% 3;
                        if ( (k == 0) && ((L.getStatus(i,j) != 0)&& (L.getStatus(i,j) != 1)) && L.getStatus(i,j) != 2)
                        {
                            while(L.getTanaman(i, j).GetAge() != 0)
                                L.getTanaman(i, j).DecAge();
                        }
                }
            }
            break;
        case 3:
            for (i=0;i<=8;i++)
            {
                for(j=0;j<=8;j++)
                {
                    if (((i==2)&&(j==3)) || ((i==2)&&(j==4)) || ((i==2)&&(j==5)) || ((i==3)&&(j==2)) || ((i==3)&&(j==6)) || ((i==4)&&(j==2)) || ((i==4)&&(j==6)) || ((i==5)&&(j==2)) || ((i==5)&&(j==6)) ||((i==6)&&(j==3)) || ((i==6)&&(j==4)) || ((i==6)&&(j==5)) )
                    {
                        k = L.getStatus(i,j);
                        if ((k==5) || (k==6) || (k==7))
                        {
                            while(L.getTanaman(i, j).GetAge() != 0)
                                L.getTanaman(i, j).DecAge();
                        }
                        else if ((k==3) || (k==4) || (k==1))
                        {
                            L.setStatus(i,j,2);
                        }
                    }
                }
            }
            break;
        case 4:
            for (i=0;i<=8;i++)
            {
                for(j=0;j<=8;j++)
                {
                    if((L.getStatus(i,j) != 0) && (L.getStatus(i,j) != 1) && (L.getStatus(i,j) != 2) && L.getStatus(i,j) != 8)
                    {
                        L.getTanaman(i,j).SetHappyMeter(20);
                    }
                }
            }
            break;
        case 6:
            for (i=0;i<=8;i++)
            {
                for(j=0;j<=8;j++)
                {
                    k = L.getStatus(i,j);
                    if (k==3)
                    {
                        L.setStatus(i,j,4);
                    }
                    else if (k==5)
                    {
                        L.setStatus(i,j,6);
                    }
                }
            }
            break;
        default:
            break;
    }
}

void Times::SetClock(int newClock)
{
    Clock = newClock;
}

void Times::GetSDay()
{
    int a;
    a = NormalSeason - Day;
    cout << a << " hari menjelang pergantian musim..." << endl;
}

void Times::GetSSeason()
{
    switch (Season)
    {
        case 0 :
            cout<<"Spring          ";
            break;
        case 1:
            cout<<"Summer          ";
            break;
        case 2:
            cout<<"Fall            ";
            break;
        case 3:
            cout<<"Winter          ";
            break;
    }
}

void Times::GetSEvent()
{
    switch (Event)
    {
        case 0 :
            cout<<"It's 100% sunny..           ";
            break;
        case 1:
            cout<<"It's raining outside..      ";
            break;
        case 2:
            cout<<"A tornado strikes..         ";
            break;
        case 3:
            cout<<"Warning! Alien attack!!     ";
            break;
        case 4:
            cout<<"A magical event occurs..    ";
            break;
        case 5 :
            cout<<"It's 100% sunny..           ";
            break;
        case 6 :
            cout<<"It's raining outside..      ";
            break;
    }
}

void Times::GetSClock()
{
    int a;
    a = NormalDay - Clock;
    cout << a << " time units before night...";
}

void Times::ChangeDay()
{
    I.HargaNetral();
    Clock = 0;
    Day += 1;
    for (int i = 0; i <= 8; i++)
        for (int j = 0; j <= 8; j++)
        {
            if(L.getStatus(i,j) == 4)
            {
                L.setStatus(i,j,3);
                L.getTanaman(i,j).SetHappyMeter(5);
            }
            else if (L.getStatus(i,j) == 6)
            {
                L.setStatus(i,j,5);
                L.getTanaman(i,j).SetHappyMeter(5);
            }

            if(L.getStatus(i,j) >= 3 && L.getStatus(i,j) <= 7)
            {
                L.getTanaman(i,j).DecAge();
            }
        }
    //Tanaman::SetHappyMeter(-1);
    //Tanaman::DecAge();
    if (Day == NormalSeason)
    {
       ChangeSeason();
    }
}

void Times::ChangeSeason()
{
    Day = 0;
    if (Season==3)
    {
        Season=0;
    }
    else
    {
        Season += 1;
    }
    int i, j;
    for(i = 1; i <= 7; i++)
    {
        for(j = 1; j <= 7; j++)
        {
            if ((L.getStatus(i, j) >= 3) && (L.getStatus(i, j) <= 7))
            {
                int musim = L.getTanaman(i, j).GetMusimable();
                switch(Season)
                {
                    case 0:
                        if ((musim != 0) && (musim != 4) && (musim != 5))
                            while(L.getTanaman(i, j).GetAge() != 0)
                                L.getTanaman(i, j).DecAge();
                        break;
                    case 1:
                        if ((musim != 1) && (musim != 4) && (musim != 5) && (musim != 6))
                            while(L.getTanaman(i, j).GetAge() != 0)
                                L.getTanaman(i, j).DecAge();
                        break;
                    case 2:
                        if ((musim != 2) && (musim != 4) && (musim != 6))
                            while(L.getTanaman(i, j).GetAge() != 0)
                                L.getTanaman(i, j).DecAge();
                        break;
                    case 3:
                        if ((musim != 3) && (musim != 4))
                            while(L.getTanaman(i, j).GetAge() != 0)
                                L.getTanaman(i, j).DecAge();
                        break;
                }
            }
        }
    }
}

void Times::IncClock()
{
    Clock += 1;
    if (A.GetStatus() == 1)
    {
        A.Move();
    }
    if (B.GetStatus() == 1)
    {
        B.Move();
    }
    if (C.GetStatus() == 1)
    {
        C.Move();
    }
    if(Clock == NormalDay)
    {
        cout << "You fell asleep..." << endl;
        Sleep(1000);
        P.Teleport(0);
        ChangeDay();
        RandomEvent();
    }
}

void Times::RandomEvent()
{
    int i,j,k;
    if ((Day % 5) == 0)
    {
        Event = rand()%7;
    }
    else
    {
        Event = 0;
    }
    switch(Event)
    {
        case 1:
            for (i=0;i<=8;i++)
            {
                for(j=0;j<=8;j++)
                {
                    k = L.getStatus(i,j);
                    if (k==3)
                    {
                        L.setStatus(i,j,4);
                    }
                    else if (k==5)
                    {
                        L.setStatus(i,j,6);
                    }
                }
            }
            break;
        case 2:
            for (i=0;i<=8;i++)
            {
                for(j=0;j<=8;j++)
                {
                        k = rand ()% 3;
                        if ( (k == 0) && ((L.getStatus(i,j) != 0)&& (L.getStatus(i,j) != 1)) && L.getStatus(i,j) != 2)
                        {
                            while(L.getTanaman(i, j).GetAge() != 0)
                                L.getTanaman(i, j).DecAge();
                        }
                }
            }
            break;
        case 3:
            for (i=0;i<=8;i++)
            {
                for(j=0;j<=8;j++)
                {
                    if (((i==2)&&(j==3)) || ((i==2)&&(j==4)) || ((i==2)&&(j==5)) || ((i==3)&&(j==2)) || ((i==3)&&(j==6)) || ((i==4)&&(j==2)) || ((i==4)&&(j==6)) || ((i==5)&&(j==2)) || ((i==5)&&(j==6)) ||((i==6)&&(j==3)) || ((i==6)&&(j==4)) || ((i==6)&&(j==5)) )
                    {
                        k = L.getStatus(i,j);
                        if ((k==5) || (k==6) || (k==7))
                        {
                            while(L.getTanaman(i, j).GetAge() != 0)
                                L.getTanaman(i, j).DecAge();
                        }
                        else if ((k==3) || (k==4) || (k==1))
                        {
                            L.setStatus(i,j,2);
                        }
                    }
                }
            }
            break;
        case 4:
            for (i=0;i<=8;i++)
            {
                for(j=0;j<=8;j++)
                {
                    if((L.getStatus(i,j) != 0) && (L.getStatus(i,j) != 1) && (L.getStatus(i,j) != 2) && L.getStatus(i,j) != 8)
                    {
                        L.getTanaman(i,j).SetHappyMeter(20);
                    }
                }
            }
            break;
        case 6:
            for (i=0;i<=8;i++)
            {
                for(j=0;j<=8;j++)
                {
                    k = L.getStatus(i,j);
                    if (k==3)
                    {
                        L.setStatus(i,j,4);
                    }
                    else if (k==5)
                    {
                        L.setStatus(i,j,6);
                    }
                }
            }
            break;
        default:
            break;
    }
}

void Times::PrintTimes()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x70);
    cout << "Time\t: ";
    GetSClock();
    cout << "\t|" << endl;
    cout << "Day\t: " << Day;
    cout << "\t\t\t\t|" << endl;
    cout << "Season\t: ";
    GetSSeason();
    cout << "\t\t|" << endl;
    cout << "Event\t: ";
    GetSEvent();
    cout << "\t|" << endl;
    cout << "________________________________________|" << endl;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
}

void Times::UpdateTimes()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x70);
    M.gotoxy(10,0);
    GetSClock();
    M.gotoxy(10,1);
    cout << Day;
    M.gotoxy(10,2);
    GetSSeason();
    M.gotoxy(10,3);
    GetSEvent();
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
}
